<template>
  <div>
    <!-- 表单 -->
    <TopFilter ref="form" :cols="2" :list="formList" @filterChange="formChange" @onCollapse="collapseHandle"></TopFilter>
    <!-- 表格 -->
    <FilterTable
      ref="table"
      height="200"
      :columns="columns"
      columnsRef="partPartInventoryPartInventoryComponentsPartReturn"
      :dataSource="dataSource"
      :onColumnsChange="columns => (this.columns = columns)"
      :onEnterEvent="onEnterEvent"
      :fetchapi="fetchApi"
      selectionType="single"
      :params="fetchParams"
      :datakey="'rows'"
    ></FilterTable>
    <FilterTable
      ref="table2"
      height="auto"
      selectionType="single"
      :columns="columns2"
      columnsRef="partPartInventoryPartInventoryComponentsPartReturn1"
      :dataSource="dataSource2"
      :onCellChange="onCellChange"
      :onColumnsChange="columns2 => (this.columns2 = columns2)"
      :onRowSelectChange="handleTable2SelectChange"
    >
      <template slot="moreActions">
        <span @click="deleteBtnClick">删除数据</span>
      </template>
    </FilterTable>
    <div class="btnWrap">
      <el-button size="small" @click="$emit('cancel')">关 闭</el-button>
      <el-button type="primary" size="small" @click="confirmBtnClick">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { state, mutations } from './../store';
import { partbuyQueryForReturn } from '@/api/part/partInventory/partInventory/index.js';
export default {
  name: 'cgrk_part_return',
  props: {
    customerCode: String
  },
  data() {
    return {
      flag: false,
      formList: this.createList(),
      columns: this.createColumns(),
      columns2: this.createColumns2(),
      dataSource: [],
      dataSource2: [],
      fetchParams: { xhrAbort: true },
      fetchApi: partbuyQueryForReturn,
      selectTable2List: [] // 第二个table选中的数据
    };
  },
  mounted() {
    window.cgrk_part_return = this;
    this.formList.find(x => x.fieldName == 'customerCode').initialValue = this.customerCode;
  },
  methods: {
    createList() {
      return [
        {
          type: 'INPUT',
          label: '入库单号',
          fieldName: 'stockInNo'
        },
        {
          type: 'INPUT',
          label: '客户编号',
          fieldName: 'customerCode',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo'
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName'
        },
        {
          type: 'SELECT', // 原 INPUT
          label: '仓库',
          fieldName: 'storageCode',
          itemList: state.dict_ck
        },
        {
          type: 'RANGE_DATE',
          label: '入账日期',
          fieldName: 'dateFrom|dateTo'
        }
        // 原 {
        //   type: 'INPUT',
        //   label: '入账日期开始',
        //   fieldName: 'dateFrom'
        // },
        // {
        //   type: 'INPUT',
        //   label: '入账日期结束',
        //   fieldName: 'dateTo'
        // }
      ];
    },
    createColumns() {
      return [
        {
          title: '序号',
          sorter: true,
          width: 70,
          dataIndex: 'index',
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '入库单号',
          // sorter: true,
          dataIndex: 'STOCK_IN_NO',
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库',
          // sorter: true,
          dataIndex: 'STORAGE_NAME',
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          // sorter: true,
          dataIndex: 'PART_NO',
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          // sorter: true,
          dataIndex: 'PART_NAME',
          filter: true,
          filterType: 'input'
        },
        {
          title: '入库数量',
          sorter: true,
          dataIndex: 'IN_QUANTITY',
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税单价',
          sorter: true,
          dataIndex: 'IN_PRICE',
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税金额',
          sorter: true,
          dataIndex: 'IN_AMOUNT',
          filter: true,
          filterType: 'number'
        },
        {
          title: '含税单价',
          sorter: true,
          dataIndex: 'IN_PRICE_TAXED',
          filter: true,
          filterType: 'number'
        },
        {
          title: '含税金额',
          sorter: true,
          dataIndex: 'IN_AMOUNT_TAXED',
          filter: true,
          filterType: 'number'
        }
      ];
    },
    createColumns2() {
      return [
        {
          title: '序号',
          sorter: true,
          width: 70,
          dataIndex: 'index',
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        // {
        //   title: '操作',
        //   dataIndex: 'column-action',
        //   minWidth: 100,
        //   render: props => {
        //     return (
        //       <div>
        //         <el-button onClick={() => this.deleteBtnClick(props)} type="text" size="mini">
        //           删除
        //         </el-button>
        //       </div>
        //     );
        //   }
        // },
        {
          title: '入库单号',
          // sorter: true,
          dataIndex: 'STOCK_IN_NO',
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库',
          // sorter: true,
          dataIndex: 'STORAGE_NAME',
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          // sorter: true,
          dataIndex: 'PART_NO',
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          // sorter: true,
          dataIndex: 'PART_NAME',
          filter: true,
          filterType: 'input'
        },
        {
          title: '退货数量',
          // sorter: true,
          dataIndex: 'IN_QUANTITY',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          filter: true,
          filterType: 'number'
        },
        {
          title: '可退货数量',
          // sorter: true,
          dataIndex: 'CAN_RETURN_QUANTITY',
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税单价',
          // sorter: true,
          dataIndex: 'IN_PRICE',
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税金额',
          // sorter: true,
          dataIndex: 'IN_AMOUNT',
          filter: true,
          filterType: 'number'
        },
        {
          title: '含税单价',
          // sorter: true,
          dataIndex: 'IN_PRICE_TAXED',
          filter: true,
          filterType: 'number'
        },
        {
          title: '含税金额',
          // sorter: true,
          dataIndex: 'IN_AMOUNT_TAXED',
          filter: true,
          filterType: 'number'
        }
      ];
    },
    deleteBtnClick(props) {
      // const index = props.row.$index;
      // this.dataSource2.splice(index, 1);
      this.$refs.table2.EXECUTE_DELETE(this.selectTable2List);
    },
    onCellChange(val, val2) {
      if (Number(this.dataSource2[val2.$index].IN_QUANTITY) > Number(this.dataSource2[val2.$index].CAN_RETURN_QUANTITY)) {
        this.$notify({
          title: '警告',
          message: '退货数量不能大于可退货数量',
          type: 'warning'
        });
      }
      if (isNaN(Number(this.dataSource2[val2.$index].IN_QUANTITY))) {
        this.dataSource2[val2.$index].IN_QUANTITY = 0;
      }
      this.dataSource2[val2.$index].IN_AMOUNT = Number(this.dataSource2[val2.$index].IN_QUANTITY) * Number(this.dataSource2[val2.$index].IN_PRICE);
      this.dataSource2[val2.$index].IN_AMOUNT = this.dataSource2[val2.$index].IN_AMOUNT.toFixed(2);
      this.dataSource2[val2.$index].IN_AMOUNT_TAXED = Number(this.dataSource2[val2.$index].IN_QUANTITY) * Number(this.dataSource2[val2.$index].IN_PRICE_TAXED);
      this.dataSource2[val2.$index].IN_AMOUNT_TAXED = this.dataSource2[val2.$index].IN_AMOUNT_TAXED.toFixed(2);
      this.dataSource2 = [...this.dataSource2];
    },
    // 下方table选中
    handleTable2SelectChange(e) {
      this.selectTable2List = e;
    },
    collapseHandle(val) {
      //  this.$nextTick(() => {
      //  this.BaseTable.EXECUTE_RESET_HEIGHT();
      //  });
    },
    formChange(val) {
      console.log('formChange', val);
      val.dateFrom = val.dateFrom ? val.dateFrom.split(' ')[0] + '00:00:00' : '';
      val.dateTo = val.dateTo ? val.dateTo.split(' ')[0] + '23:59:59' : '';
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
      // const res = await partbuyQueryForReturn(val)
      // this.dataSource = res.data.rows
      // console.log('查询可退货单号', res)
    },
    // 配件 双击事件
    onEnterEvent(row) {
      console.log('选中配件 带到下一行', row);
      // const flag1 = this.dataSource.find(item => {
      //     return item['STOCK_IN_NO'] === row['STOCK_IN_NO'];
      //   });
      //   const flag2 = this.dataSource.find(item => {
      //     return item['PART_NO'] === row['PART_NO'];
      //   });
      let flag1 = false;
      let flag2 = false;
      this.dataSource2.forEach(item => {
        if (item['STOCK_IN_NO'] === row['STOCK_IN_NO']) {
          flag1 = true;
        }
        if (item['PART_NO'] === row['PART_NO']) {
          flag2 = true;
        }
        if (flag1 && flag2) {
          this.flag = true;
        }
      });
      if (this.flag) {
        this.$notify({
          title: '警告',
          message: '配件已存在',
          type: 'warning'
        });
        this.flag = false;
        return;
      }
      const _row = Object.assign({}, row, {
        OLD_STOCK_IN_NO: row['STOCK_IN_NO'],
        RETURN_ITEM_ID: row['ITEM_ID']
      });
      // 不含税单价 IN_PRICE
      // 不含税金额 IN_AMOUNT
      // 含税单价 IN_PRICE_TAXED
      // 含税金额 IN_AMOUNT_TAXED
      // 这里计算
      // 然后写一个计算属性的方法
      let tax = Number(state.tax);
      // _row.IN_QUANTITY = _row.CAN_RETURN_QUANTITY; // 数量
      _row.IN_QUANTITY = 1; // 默认退库数量依据要求修改为1 hxj 2020/1/13
      _row.IN_AMOUNT = Number(_row.IN_QUANTITY) * Number(_row.IN_PRICE);
      _row.IN_AMOUNT = _row.IN_AMOUNT.toFixed(2);
      // _row.IN_PRICE_TAXED = Number(_row.IN_PRICE) * Number(tax + 1);
      // _row.IN_PRICE_TAXED = _row.IN_PRICE_TAXED.toFixed(2);
      _row.IN_AMOUNT_TAXED = Number(_row.IN_QUANTITY) * Number(_row.IN_PRICE_TAXED);
      _row.IN_AMOUNT_TAXED = _row.IN_AMOUNT_TAXED.toFixed(2);
      _row.isflg = true;
      this.dataSource2.push(_row);
      this.dataSource2 = [...this.dataSource2];
    },
    confirmBtnClick() {
      if (this.dataSource2.length == 0) {
        this.$notify({
          title: '警告',
          message: '退货数据未选择',
          type: 'warning'
        });
        return;
      }
      let pdOne = false;
      let pdTwo = false;
      this.dataSource2.forEach(x => {
        if (Number(x.IN_QUANTITY) > Number(x.CAN_RETURN_QUANTITY)) {
          pdOne = true;
        }
        if (Number(x.IN_QUANTITY) < 0.01) {
          pdTwo = true;
        }
      });
      if (pdOne) {
        this.$notify({
          title: '警告',
          message: '退货数量不能大于可退货数量',
          type: 'warning'
        });
        return;
      }
      if (pdTwo) {
        this.$notify({
          title: '警告',
          message: '请输入不小于0.01的数值',
          type: 'warning'
        });
        return;
      }
      const _row = this.dataSource2.map(item => {
        const item2 = Object.assign({}, item);
        return item2;
      });
      console.log('确定', _row);
      this.$emit('confirm', _row);
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
